﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BgControllor : MonoBehaviour
{
    // Start is called before the first frame update
    Camera mainCamera ;
    private float length = 9;
    private float total_length;
    void Start()
    {
        mainCamera = Camera.main;
        total_length = 3*length * this.transform.localScale.x;
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 bgPosition = this.transform.position;
        Vector2 cameraPosition = mainCamera.transform.position; // mainCamera是相机的位置
        if (bgPosition.x + total_length / 2.0f < cameraPosition.x)
        {
            bgPosition.x += total_length;
            this.transform.position = bgPosition;
        }
        if (bgPosition.x - total_length / 2.0f > cameraPosition.x)
        {
            bgPosition.x -= total_length;
            this.transform.position = bgPosition;
        }
    }
}
